﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;

namespace Protobuf_Gui.Domain
{
    /// <summary>
    /// message Name
    /// {
    ///     message fields
    ///     inner messages
    /// }
    /// </summary>
    [Serializable]
    [ContainType(typeof(PbMessageField),"Message field")]
    [ContainType(typeof(PbMessage),"Message")]
    [ContainType(typeof(PbEnum),"Enum")]
    public class PbMessage : ProtocolElement
    {
        public PbMessage(string name) : base(name)
        {
            MessageFields = new List<PbMessageField>();
            Messages = new List<PbMessage>();
            Enums = new List<PbEnum>();
        }

        public List<PbMessageField> MessageFields { get; set; }

        public List<PbMessage> Messages { get; set; }

        public List<PbEnum> Enums { get; set; }

        public override TreeNode ToTreeNode(TreeNode root)
        {
            throw new NotImplementedException();
        }

        public override void ToFile(TextWriter writer, uint nesting)
        {
            string tabs = Tabs(nesting);
            writer.WriteLine("{0} message {1}", tabs, Name);
            writer.WriteLine(tabs + "{");

            foreach (var @enum in Enums)
            {
                @enum.ToFile(writer, nesting + 1);
            }

            foreach (var messageField in MessageFields)
            {
                messageField.ToFile(writer, nesting + 1);
            }

            foreach (var message in Messages)
            {
                message.ToFile(writer, nesting + 1);
            }


            writer.WriteLine(tabs + "}");
        }
    }
}